package com.whiteape.book.ui.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.whiteape.book.R;
import com.whiteape.book.model.dto.CollectionMediaBean;
import com.whiteape.book.model.dto.DownloadTaskBean;
import com.whiteape.book.model.type.DownloadStatus;
import com.whiteape.book.model.type.MediaType;
import com.whiteape.book.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class DownloadAdapter extends BaseQuickAdapter<DownloadTaskBean, BaseViewHolder> {

    List<DownloadTaskBean> mSelectedList = new ArrayList<>();
    boolean selectMode = false;
    public DownloadAdapter(int layoutResId, @Nullable List<DownloadTaskBean> data) {
        super(layoutResId, data);
    }

    public void setSelectedList(List<DownloadTaskBean> mSelectedList) {
        this.mSelectedList = mSelectedList;
    }

    public void setSelectMode(boolean selectMode) {
        this.selectMode = selectMode;
    }

    @Override
    protected void convert(BaseViewHolder helper, DownloadTaskBean item) {

        CollectionMediaBean mediaBean = item.getCollectionMediaBean();
        helper.setGone(R.id.iv_select, selectMode);
        if (mSelectedList.contains(item)) {
            helper.setImageResource(R.id.iv_select, R.mipmap.icon_mine_has_selected);
            ((ImageView)helper.getView(R.id.iv_select)).setColorFilter(mContext.getResources().getColor(R.color.colorAccent));
        } else {
            ((ImageView)helper.getView(R.id.iv_select)).clearColorFilter();
            helper.setImageResource(R.id.iv_select, R.mipmap.icon_mine_not_select);
        }
        helper.setText(R.id.download_tv_media_type, MediaType.getName(mediaBean.getMediaType()));
        helper.setText(R.id.download_tv_progress_total, "总进度：" + item.getCurrentChapter() + "/" + item.getMediaChapterList().size());
        helper.setText(R.id.download_tv_title, mediaBean.getTitle());
        ProgressBar progressBar = helper.getView(R.id.download_pb_show);

        //图片不同的时候，才重新加载，否则可能会闪烁
        //glide不允许在他的imageview上添加tag,我们添加到外面一层的framelayout就行，只做记录使用
        if (!mediaBean.getCoverUrl().equals(helper.getView(R.id.fl_img).getTag())) {
            helper.setTag(R.id.fl_img, mediaBean.getCoverUrl());
            Glide.with(mContext)
                    .load(mediaBean.getCoverUrl())
                    .into((ImageView) helper.getView(R.id.download_iv_cover));
        }

        setProgressMax(progressBar, item);
        progressBar.setProgress(item.getCurrentChapter());
        switch (item.getStatus()) {
            case DownloadStatus.STATUS_DOWNLOADING:
                changeBtnStyle(helper,R.string.wa_download_pause,
                        R.color.download_pause,R.mipmap.ic_download_pause);

                int mediaType = item.getCollectionMediaBean().getMediaType();
                if (mediaType == MediaType.MEDIA_TYPE_NOVEL) {
                    int index = item.getCurrentChapter();
                    index = index >= item.getMediaChapterList().size() ? item.getMediaChapterList().size() - 1 : index;
                    helper.setText(R.id.download_tv_progress_current, "正在下载：" + item.getMediaChapterList().get(index).getName());
                } else if (mediaType == MediaType.MEDIA_TYPE_COMIC) {
                    int index = item.getCurrentChapter();
                    index = index >= item.getMediaChapterList().size() ? item.getMediaChapterList().size() - 1 : index;
                    helper.setText(R.id.download_tv_progress_current, "正在下载：" + item.getMediaChapterList().get(index).getName()
                            + "(" + (item.getCurrentChapterCurPart() + 1) + "/" + item.getCurrentChapterPartTotal() + ")");
                }
                helper.setTextColor(R.id.download_tv_progress_current, mContext.getResources().getColor(R.color.download_loading));
                break;
            case DownloadStatus.STATUS_WAIT:
                changeBtnStyle(helper, R.string.wa_download_wait,
                        R.color.download_wait,R.mipmap.ic_download_wait);

                changeCurrentTip(helper, R.string.wa_download_waiting, R.color.download_wait);
                break;
            case DownloadStatus.STATUS_PAUSE:
                changeBtnStyle(helper, R.string.wa_download_start,
                        R.color.download_loading,R.mipmap.ic_download_loading);
                changeCurrentTip(helper, R.string.wa_download_pausing, R.color.download_pause);

                break;
            case DownloadStatus.STATUS_FINISH:
                changeBtnStyle(helper, R.string.wa_download_finish,
                        R.color.download_finish,R.mipmap.ic_download_complete);
                changeCurrentTip(helper, R.string.wa_download_complete, R.color.download_finish);
                break;
            case DownloadStatus.STATUS_ERROR:
                changeBtnStyle(helper, R.string.wa_download_retry,
                        R.color.download_loading,R.mipmap.ic_download_loading);
//                changeBtnStyle(helper, R.string.wa_download_error,
//                        R.color.download_error,R.mipmap.ic_download_error);
                changeCurrentTip(helper, R.string.wa_download_source_error, R.color.download_error);
                break;

        }
        helper.addOnClickListener(R.id.download_rl_right);
        helper.addOnClickListener(R.id.fl_img);
        helper.addOnClickListener(R.id.download_rl_left);

    }

    private void changeBtnStyle(BaseViewHolder holder, int strRes,int colorRes,int drawableRes){
        //按钮状态
        TextView mTvStatus = holder.getView(R.id.download_tv_status);
        ImageView mIvStatus = holder.getView(R.id.download_iv_status);
        if (!mTvStatus.getText().equals(
                StringUtils.getString(strRes))){
            mTvStatus.setText(StringUtils.getString(strRes));
            mTvStatus.setTextColor(mContext.getResources().getColor(colorRes));
            mIvStatus.setImageResource(drawableRes);
        }
    }

    private void changeCurrentTip(BaseViewHolder helper, int strRes, int colorRes) {
        helper.setText(R.id.download_tv_progress_current, strRes);
        helper.setTextColor(R.id.download_tv_progress_current, mContext.getResources().getColor(colorRes));
    }

    private void setProgressMax(ProgressBar mPbShow, DownloadTaskBean downloadTaskBean){
        if (mPbShow.getMax() != downloadTaskBean.getMediaChapterList().size()){
            mPbShow.setVisibility(View.VISIBLE);
            mPbShow.setMax(downloadTaskBean.getMediaChapterList().size());
        }
    }
}
